import { _decorator, Component, instantiate, Node } from 'cc';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
import { GAME_PREFAB, GlobalData } from '../../../Data/GlobalData';
import { SinglePao } from './SinglePao';
import { GunInfo } from '../../../Data/GlobaInterface';
import { EnemyLoad } from '../ZumaLoad/EnemyLoad';
const { ccclass, property } = _decorator;

@ccclass('MiddleLoad')
export class MiddleLoad extends BaseComponent {

    //大炮数组
    private GunArray: Node[] = [];

    //没有用的数组
    private NouseArray: Node[] = [];

    //敌人层
    @property(Node)
    private EnemyLoad: Node = null;

    protected OnShow(): void {

        for (let gun of this.GunArray) {
            gun.active = false;
            gun.setParent(null);
            this.NouseArray.push(gun);
        }

        for (let i = 0; i < GlobalData.BrickTypeNum; i++) {
            this.CreateGun(
                {
                    Num: 0,
                    Style: i
                }
            )
        }

    }

    /**
     * 创建大炮
     */
    public CreateGun(_guninfo: GunInfo): void {
        if (this.NouseArray.length > 0) {
            this.NouseArray[0].setParent(null);
            this.NouseArray[0].setParent(this.node);
            this.NouseArray[0].getComponent(SinglePao).UpdateGunNum(_guninfo.Num);
            this.NouseArray[0].getComponent(SinglePao).UpdateGunStyle(_guninfo.Style);
            this.GunArray.push(this.NouseArray[0])
            this.NouseArray[0].active = true;
            this.NouseArray.shift();
            return;
        }

        let sp: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.SINGLEPAO]);
        sp.setParent(this.node);
        sp.getComponent(SinglePao).UpdateGunNum(_guninfo.Num);
        sp.getComponent(SinglePao).UpdateGunStyle(_guninfo.Style);
        this.GunArray.push(sp);
    }

    /**
     * 添加炮弹
     */
    public AddBullet(_guninfor: GunInfo) {
        for (let gun of this.GunArray) {
            gun.getComponent(SinglePao).AddGunNum(_guninfor);
        }
    }

    /**
     * 发射炮弹
     */
    public FireBullet() {
        for (let gun of this.GunArray) {
            if (gun.getComponent(SinglePao).GunInfo.Num > 0) {
                for (let i = 0; i < gun.getComponent(SinglePao).GunInfo.Num; i++) {
                    this.EnemyLoad.getComponent(EnemyLoad).UpdateEnemy(gun);
                }
            }
        }
    }

}


